﻿<common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="GComp.Apps.EasyStock.Controls.AddNewPage"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:GComp.Apps.EasyStock.Controls"
    xmlns:common="using:GComp.Apps.EasyStock.Common"
    xmlns:utils="using:GComp.Apps.EasyStock.Utils"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <utils:IsNotEmptyConverter x:Key="notEmptyConverter" />
        <!-- Collection of items displayed by this page -->
        <CollectionViewSource
            x:Name="itemsViewSource"
            Source="{Binding Items}"/>
    </Page.Resources>

    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid Style="{StaticResource LayoutRootStyle}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Grid Grid.Row="2" Grid.Column="1" Width="450">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.ColumnSpan="3" Text="Add new group" FontSize="25" HorizontalAlignment="Center" />
            
            <TextBlock Grid.Row="1" TextWrapping="Wrap" Text="Group name" Margin="4" Style="{StaticResource BasicTextStyle}" />
            <TextBlock Grid.Row="2" TextWrapping="Wrap" Text="Description" Margin="4" Style="{StaticResource BasicTextStyle}" />

            <Button Name="btnOKGroup" Content="OK" Margin="4" Grid.Row="3" Grid.Column="1" Click="OnBtnOKGroups_Click" VerticalAlignment="Top" 
                    IsEnabled="{Binding ElementName=tbGroupTitle, Path=Text, Converter={StaticResource notEmptyConverter}}" />
            <Button Name="btnClearGroup" Content="Clear" Margin="4" Grid.Column="2" Grid.Row="3" VerticalAlignment="Top" Click="btnClearGroup_Click" />
            <TextBox Name="tbGroupTitle" TextWrapping="Wrap" Text="" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" Margin="4" />
            <TextBox Name="tbGroupDescription" TextWrapping="Wrap" Text="" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2" Margin="4" />
        </Grid>

        <Grid Grid.Row="2" Grid.Column="3" Height="Auto" Width="450">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.ColumnSpan="3" Text="Add new stock" FontSize="25" HorizontalAlignment="Center" />

            <TextBlock Grid.Row="1" TextWrapping="Wrap" Text="Group" Margin="4" Style="{StaticResource BasicTextStyle}" />
            <ComboBox Name="cbGroups" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Margin="4" ItemsSource="{Binding}" SelectionChanged="cbGroups_SelectionChanged_1">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Title}" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

            <TextBlock Grid.Row="2" Text="Self defined title" Style="{StaticResource BasicTextStyle}" />
            <TextBox Name="tbTitle" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Margin="4" />

            <TextBlock Grid.Row="3" TextWrapping="Wrap" Text="exact Symbol name" Margin="4" Style="{StaticResource BasicTextStyle}" />
            <TextBox Name="tbSymbol" Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2" Margin="4" Text="{Binding ElementName=lvResults, Path=SelectedItem.Symbol}" TextChanged="tbSymbol_TextChanged_1" />

            <TextBlock Grid.Row="4" TextWrapping="Wrap" Text="or search by part of name or symbol" Margin="4" Style="{StaticResource BasicTextStyle}" Width="120" />
            <TextBox Name="tbSearch" TextWrapping="Wrap" Text="" Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="2" Margin="4" VerticalAlignment="Center" />
            <Button Name="btnSearch" Grid.Row="4" Grid.Column="3" Content="Search" Click="btnSearch_Click" 
                    IsEnabled="{Binding ElementName=tbSearch, Path=Text, Converter={StaticResource notEmptyConverter}}" />

            <ListView Name="lvResults" Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="3" Height="150" ItemsSource="{Binding}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Name}" Style="{StaticResource BasicTextStyle}" />
                            <TextBlock Text=", " Style="{StaticResource BasicTextStyle}" />
                            <TextBlock Text="{Binding Symbol}" Style="{StaticResource BasicTextStyle}" />
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

            <Button Name="btnOkStock" Content="OK" Margin="4" Grid.Row="6" Grid.Column="1" Click="btnOkStock_Click" IsEnabled="False" />
            <Button Name="btnClearStock" Content="Clear" Margin="4" Grid.Column="2" Grid.Row="6" />
        </Grid>

        <Grid Grid.Row="4" Grid.Column="1" Width="450">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.ColumnSpan="3" Text="Delete group" FontSize="25" HorizontalAlignment="Center" />

            <TextBlock Grid.Row="1" TextWrapping="Wrap" Text="Group name" Margin="4" Style="{StaticResource BasicTextStyle}" />
            <ComboBox Name="cbDeleteGroups" Grid.Row="1" Grid.Column="1" Margin="4" ItemsSource="{Binding}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Title}" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

            <Button Name="btnDeleteGroup" Content="Delete" Margin="4" Grid.Row="1" Grid.Column="2" Click="btnDeleteGroup_Click_1" VerticalAlignment="Top"
                    IsEnabled="{Binding ElementName=cbDeleteGroups, Path=SelectedItem, Converter={StaticResource notEmptyConverter}}" />
        </Grid>

        <Grid Grid.Row="4" Grid.Column="3" Width="450">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.ColumnSpan="3" Text="Delete stocks" FontSize="25" HorizontalAlignment="Center" />

            <TextBlock Grid.Row="1" TextWrapping="Wrap" Text="Stock name" Margin="4" Style="{StaticResource BasicTextStyle}" />
            <ComboBox Name="cbDeleteStocks" Grid.Row="1" Grid.Column="1" Margin="4" ItemsSource="{Binding}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Path=Group.Title}" />
                            <TextBlock Text=" - " />
                            <TextBlock Text="{Binding Path=Title}" />
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

            <Button Name="btnDeleteStock" Content="Delete" Margin="4" Grid.Row="1" Grid.Column="2" Click="btnDeleteStock_Click" VerticalAlignment="Top" 
                    IsEnabled="{Binding ElementName=cbDeleteStocks, Path=SelectedItem, Converter={StaticResource notEmptyConverter}}" />
        </Grid>

        <!-- Back button and page title -->
        <Grid Grid.ColumnSpan="5">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
            <TextBlock x:Name="pageTitle" Text="{Binding Group.Title}" Style="{StaticResource PageHeaderTextStyle}" Grid.Column="1" IsHitTestVisible="false"/>
        </Grid>

        <StackPanel Grid.Row="6" Grid.ColumnSpan="5" Name="stackPanel" Orientation="Horizontal">
            <TextBlock Text="Background operations:" Style="{StaticResource BasicTextStyle}" Margin="4" />
            <TextBlock Name="tbDownloadState" Text="{Binding DownloadState}" Style="{StaticResource BasicTextStyle}" Margin="4" />
        </StackPanel>

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- The back button respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!-- The back button and title have different styles when snapped -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>
